/**
 * 
 */
package com.ntv.persistent.news.sql.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import com.ntv.domain.INews;
import com.ntv.domain.News;
import com.ntv.persistent.AbstractDao;
import com.ntv.persistent.PersistentException;
import com.ntv.persistent.news.INewsDao;
import com.ntv.persistent.news.sql.impl.NewsDao.CONST.QUERY;

/**
 * @author Emperor
 * 
 */
public class NewsDao extends AbstractDao implements INewsDao 
{
	
	private SimpleJdbcInsert insertNews;

	interface CONST {
		interface QUERY {

			String GET_NEWS_QUEUEY = "SELECT * FROM NEWS ORDER BY DATE_OF_UPDATE";
			String GET_LATEST_NEWS_QUEUEY = "SELECT * FROM NEWS a, NEWS_TYPE b, NEWS_TYPE_MAP c WHERE published_on =:publishedOn";
			
			String GET_NATIONAL_NEWS_QUEUEY = "select " +
			" a.news_id, " +
			" a.location," +
			" a.complete_news," +
			" a.head_line, " +
			" a.date published_on," +			
			" a.date_of_creation,"+			
			" a.date_of_update,"+
			" a.content_id,"+
			" b.type_id," +			
			" b.name news_category " +
		"from " +
			"news a, " +
			"news_types b, " +
			"news_type_map c " +
		"where " +
			"	a.news_id = c.news_id " +
			"and b.type_id = c.type_id " +
			"and b.type_id =:typeId";

			
			String GET_NEWS_BY_NEWS_TYPE= "select " +
			" a.news_id, " +
			" a.location," +
			" a.complete_news," +
			" a.head_line, " +
			" a.date published_on," +			
			" a.date_of_creation,"+
			" a.date_of_update,"+
			" a.content_id,"+			
			" b.type_id," +
			" b.name news_category " +
		"from " +
			"news a, " +
			"news_types b, " +
			"news_type_map c " +
		"where " +
			"	a.news_id = c.news_id " +
			"and b.type_id = c.type_id " +
			"and b.type_id =:typeId";
			
		String GET_NEWS_BY_NEWS_TYPEAND_NEWS_ID = "select " +
			" a.news_id, " +
			" a.location," +
			" a.complete_news," +
			" a.head_line, " +
			" a.date published_on," +			
			" a.date_of_creation,"+
			" a.date_of_update,"+
			" a.content_id,"+			
			" b.type_id," +
			" b.name news_category " +
		" from " +
			" news a, " +
			" news_types b, " +
			" news_type_map c " +
		" where " +
			" a.news_id = c.news_id " +
			" and b.type_id = c.type_id " +
			" and a.news_id =:newsId";


		}
		
	}

	private static final Logger logger = Logger.getLogger(NewsDao.class);
	

	public INews createNews(INews news) throws PersistentException {
		try {
			
			

			
			logger.info("NEWS table insertion.");
			//News table insertion.
			insertNews = new SimpleJdbcInsert(getJdbcTemplate()).withTableName(
					getTableName()).usingGeneratedKeyColumns("news_id");
			Date sysDate = new Date();
			HashMap<String, Object> parameters = new HashMap<String, Object>(10);
			parameters.put("content_id", news.getContentId());
			parameters.put("location", news.getLocation());
			parameters.put("head_line", news.getHeadLine());
			parameters.put("date", news.getPublishedOn());// FIXME
			parameters.put("complete_news", news.getCompleteNews());
			
			parameters.put("created_by", (news.getCreatedBy() == null) ? 0 : news.getCreatedBy());
			parameters.put("updated_by", news.getUpdatedBy() == null ? 0 : news.getUpdatedBy());
			parameters.put("date_of_creation",sysDate);// FIXME
			parameters.put("date_of_update", sysDate);// FIXME

			Number newId = insertNews.executeAndReturnKey(parameters);
			news.setId(newId.longValue());
			logger.info("news table insertion completed successfully.");
			
			return news;
		} catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
	}
	
	
	public List<INews> getNews() throws PersistentException 
	{
		try 
		{
			List<INews> allNews = getJdbcTemplate().query(QUERY.GET_NEWS_QUEUEY, new RowMapper() {
				public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
					INews news  = new News();
					news.setId(rs.getLong("news_id"));
					news.setPublishedOn(rs.getDate("date"));
					news.setLocation(rs.getString("location"));
					news.setHeadLine(rs.getString("head_line"));
					news.setCreatedBy(rs.getLong("created_by"));
					news.setCreatedOn(rs.getDate("date_of_creation"));
					news.setUpdatedOn(rs.getDate("date_of_update"));
					return news;
					}
					});
			logger.info("News list:"+allNews);
			return allNews;
		} 
		catch (DataAccessException ex) 
		{
			throw new PersistentException(ex);
		}
	}
	
	
	public List<INews> getNewsByNewsType(Long newsTypeId) {
		SqlParameterSource namedParameters = new MapSqlParameterSource("typeId", newsTypeId);
		List<INews> newsList = getNamedParameterJdbcTemplate().query(QUERY.GET_NEWS_BY_NEWS_TYPE, namedParameters, getRowMapper());
		return newsList;
	}
	
	
	public List<INews> getCurrentDayNews() throws PersistentException  
	{
		try
		{
			//FIXME
			//return getNamedParameterJdbcTemplate().query(GET_LATEST_NEWS_QUEUEY, new HashMap(), getRowMapper());
			return null;
		} 
		catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
	}
	
	public List<INews> getMostReadNews() throws PersistentException  {
		return null;
	}
	
	public List<INews> getLaststNewsList() throws PersistentException  {
		return null;
	}
	
	public INews getNewsByTypeAndNewsId(Long typeId, Long newsId) throws PersistentException {
		try {
			
			Map map = new HashMap<String, Long>();
			map.put("newsId", newsId);
			SqlParameterSource namedParameters = new MapSqlParameterSource(map);
			INews news = (INews)getNamedParameterJdbcTemplate().queryForObject(QUERY.GET_NEWS_BY_NEWS_TYPEAND_NEWS_ID, namedParameters,
					getRowMapper());
			logger.info(" news "  +news );
			return news;
		} 
		catch (DataAccessException ex) 
		{
			throw new PersistentException(ex);
		}
	}
	
	
}
